package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    //    导入数据层的bean
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetMealMapper setmealMapper;

//    分页查询
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO queryDTO) {
//        自动拼接上limit
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
//        数据层返回页总数和数据
        Page<Category> p = categoryMapper.pageQuery(queryDTO);
        return new PageResult(p.getTotal(), p.getResult());
    }

//    新增
    @Override
    public void add(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        // 使用aop统一处理
        //category.setCreateTime(LocalDateTime.now());
        //category.setCreateUser(BaseContext.getCurrentId());
        //category.setUpdateTime(LocalDateTime.now());
        //category.setUpdateUser(BaseContext.getCurrentId());
        category.setStatus(StatusConstant.DISABLE);
        categoryMapper.add(category);
    }

//    删除
    @Override
    public void delete(Long id) {
//        删除前看看套餐或者菜品类有没有子项
        Integer integer = dishMapper.countByCategoryID(id);
        Integer integer1 = setmealMapper.countByCategoryID(id);
        Integer category = categoryMapper.selectByIdForStatus(id);
        if (category == 1){
            throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_ENABLE_FAILED);
        }
//        如果有子项不给与删除的操作
        if (integer > 0){
            throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        } else if (integer1 > 0 ) {
            throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }else {
//            都没有就可以删除了
            categoryMapper.delete(id);
        }
    }

//    修改
    @Override
    public void edi(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        // 使用aop统一处理
        // category.setUpdateUser(BaseContext.getCurrentId());
        //category.setUpdateTime(LocalDateTime.now());
        categoryMapper.startOrBin(category);
    }

//    改状态
    @Override
    public void startOrBin(Integer status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                // 使用aop统一处理
                //.updateTime(LocalDateTime.now())
                //.updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.startOrBin(category);
    }

    // 根据菜品类型进行查询
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }
}
